import urllib.request
import urllib.parse
# urlencode 应用场景 : 多个参数的时候 转换为unicode
# https://www.baidu.com/s?wd=周杰伦&sex=男

data = {
    'wd': '周杰伦',
    'sex': '男',
    'location': '中国台湾'
}
a = urllib.parse.urlencode(data)
print(a)

import  urllib.parse
import  urllib.request

base_url = 'https://www.baidu.com/s?'
data = {
    'wd': '周杰伦',
    'sex':'男'
}
new_data = urllib.parse.urlencode(data)
# 请求资源连接
url = base_url + new_data

# 获取header
header = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76',
    'Cookie': 'BAIDUID=2E7448FA0AE22FAD41F798DA84E0B4F2:FG=1; BIDUPSID=2E7448FA0AE22FAD41F798DA84E0B4F2; PSTM=1682681731; BDUSS=3Y3NFVScmRlR3FzWjJDMkZJaEl-SDR0QW50UlR6eUV6cWM5OUFSakJvYWRQTkprSVFBQUFBJCQAAAAAAAAAAAEAAAC8s7CX7M~Oss6yAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ2vqmSdr6pkZ; BDUSS_BFESS=3Y3NFVScmRlR3FzWjJDMkZJaEl-SDR0QW50UlR6eUV6cWM5OUFSakJvYWRQTkprSVFBQUFBJCQAAAAAAAAAAAEAAAC8s7CX7M~Oss6yAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJ2vqmSdr6pkZ; BAIDUID_BFESS=2E7448FA0AE22FAD41F798DA84E0B4F2:FG=1; BAIDU_WISE_UID=wapp_1696013201973_265; ZFY=lNeXIQHPLwPfcON:AvDrOUgtBoIOW77bkUX1ZrlUfz0A:C; RT="z=1&dm=baidu.com&si=9a29f03c-e8c8-453c-af34-070eabec9368&ss=lo9wk49b&sl=2t&tt=t56&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf&ld=3h7r3&ul=8521c&hd=8523n"; __bid_n=18b8574485e531279ac5b6; BD_UPN=12314753; BA_HECTOR=ag0k2la5248g808581a4akah1ik1oja1r; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; BD_CK_SAM=1; PSINO=7; delPer=0; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; B64_BOT=1; H_PS_PSSID=39445_39396_39531_39418_39142_39439_39523_39521_39497_26350_39561_22157; shifen[7190616_91638]=1698752257; BCLID=11535887696267118712; BCLID_BFESS=11535887696267118712; BDSFRCVID=ovkOJexroG3O1Hvq8yIbUr4upFweG7bTDYrEOwXPsp3LGJLVFdWiEG0Pts1-dEu-S2OOogKK3gOTH4DF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; BDSFRCVID_BFESS=ovkOJexroG3O1Hvq8yIbUr4upFweG7bTDYrEOwXPsp3LGJLVFdWiEG0Pts1-dEu-S2OOogKK3gOTH4DF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF=tRAOoC_-tDvDqTrP-trf5DCShUFstq_jB2Q-XPoO3KOchqnCKj7BKMCAKGOtbU5f5mkf3fbgy4op8P3y0bb2DUA1y4vp0t3U2mTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9Qg-JKpQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0hD89DjKKD6PVKgTa54cbb4o2WbCQa-QO8pcN2b5oQTtgDt7qb6v8WCrPQDba-pQbeq06-lOUWJDkXpJvQnJjt2JxaqRC3JbRjq5jDh3MKToDb-oteltHB2Oy0hvctb3cShPmQMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2XjQh-p52f6_8JJ-q3j; H_BDCLCKID_SF_BFESS=tRAOoC_-tDvDqTrP-trf5DCShUFstq_jB2Q-XPoO3KOchqnCKj7BKMCAKGOtbU5f5mkf3fbgy4op8P3y0bb2DUA1y4vp0t3U2mTxoUJ2-KDVeh5Gqq-KXU4ebPRiJ-b9Qg-JKpQ7tt5W8ncFbT7l5hKpbt-q0x-jLTnhVn0MBCK0hD89DjKKD6PVKgTa54cbb4o2WbCQa-QO8pcN2b5oQTtgDt7qb6v8WCrPQDba-pQbeq06-lOUWJDkXpJvQnJjt2JxaqRC3JbRjq5jDh3MKToDb-oteltHB2Oy0hvctb3cShPmQMjrDRLbXU6BK5vPbNcZ0l8K3l02V-bIe-t2XjQh-p52f6_8JJ-q3j; COOKIE_SESSION=0_0_0_0_1_0_0_0_0_0_0_0_0_0_1119_0_1698753356_0_1698752237%7C1%230_0_1698752237%7C1; ab_sr=1.0.1_NTQxM2VhMTc2MWNlY2JhYzc5ODE3ZDdiNzY1ZGUxMjY5Njk3Mjk0YTFmNDhjOTk5NDZiODY3MTY5N2JhODRlMzY4ZGViNjRmNGEzYzE4NDk2NGNjNTU4NWYxZTc0NTMwODdmNDg1YTk0ZTBkYmZlNmZlY2E3YzM2NDYzMzYzYmY4Yjc1ZmQ2M2Q5ODIyMDY0MTJlZGJjYzAwMGJiMzMzMjQ1NTA0MDgwNmY0Mjg3YTI5NzU0Yzg4M2FkMWU2Yzhk; H_PS_645EC=77fb24RSe7MkIoHmnJb2dxH0dbUcDwyq2hszC0bmhjOlB8Fo7M77a1ObHu%2FAaq9kdCY4; BDSVRTM=580',
}
# 对象定制
request = urllib.request.Request(url = url ,headers=header)
# 构造浏览器
response = urllib.request.urlopen(request)
# 读取资源
content = response.read().decode('utf-8')

print(content)



#
# header = header = {
# 'User-Agent':
# 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
# }
#
# # 请求对象定制
# request = urllib.request.Request(url = url , headers=header)
# # 构造浏览器连接
# response = urllib.request.urlopen(url)
# content = response.read().decode('utf-8')
